<script lang="ts" setup>
import { get, post, message, loadFile } from '@/utils'
import useOptions from '@/hooks/useOptions'
const props = defineProps<{
  show: boolean
  id: number | string
}>()
const emit = defineEmits(['update:show', 'reset', 'confirm'])
const submit = done => {
  post(
    {
      url: '/server-shop/sys-seller/seller/role/grant',
      data: {
        menuIds: menu.value,
        roleIds: [props.id],
        apiScopeIds: [],
        dataScopeIds: [],
      },
    },
    ({ code, msg }) => {
      done(code == 200)
      message({
        type: code == 200 ? 'success' : 'error',
        content: msg,
      })
      emit('update:show', false)
      emit('confirm')
    },
    () => done(false)
  )
}

const treeData = ref<any[]>([])
const menu = ref<any[]>([])
const getMenuTree = () => {
  get({ url: '/server-shop/sys-seller/seller/menu/grant-tree' }, ({ data }) => {
    console.log(data)
    treeData.value = data.menu
  })
}
const getRoleMenu = () => {
  get(
    {
      url: '/server-shop/sys-seller/seller/menu/role-tree-keys',
      data: { roleIds: props.id },
    },
    ({ data }) => {
      console.log(data)
      menu.value = data.menu
    }
  )
}
onMounted(() => {
  getMenuTree()
  if (props.id) getRoleMenu()
})
</script>
<template>
  <CModal v-model:visible="props.show" @before-ok="submit" :mask-closable="false" title="菜单权限" @cancel="emit('update:show', false)">
    <div class="content">
      <a-tree
        :checkable="true"
        v-model:checked-keys="menu"
        :fieldNames="{
          key: 'id',
          title: 'title',
        }"
        :data="treeData"
      />
    </div>
  </CModal>
</template>
<style lang="less" scoped></style>
